package com.hc.controller;

import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.res.CartDetail;
import com.hc.security.jwt.JwtUtil;
import com.hc.service.CartService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author hecai
 * @date 2021/12/20 14:38
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Resource
    private CartService cartService;


    /**
     * 修改购物车信息：
     * 用户没有购物车则创建购物车，有购物车： 购物车中有sku则修改数量；没有则添加
     *
     * @param skuId
     * @param amount
     * @return
     */
    @GetMapping("/updateCart")
    public Result updateCart(Integer skuId, Integer amount) {
        boolean res = cartService.updateCart(skuId, amount);
        if (res) {
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.fail("添加失败");
    }

    @Resource
    private JwtUtil jwtUtil;

    @GetMapping("/getCart")
    public Result getCartByUserId(@RequestHeader("Authorization") String token) {
        System.out.println(token);
        String userId = jwtUtil.getUserId(token);
        CartDetail cartDetail = cartService.getCartByUserId(Integer.parseInt(userId));
        return ResultUtil.success("购物车查询成功").setData(cartDetail);
    }

}
